﻿using System;
using System.Configuration;
using System.Data;
using System.Configuration.Assemblies;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EPiServer;
using EPiServer.PlugIn;
using EPiServer.Configuration;
using EPiServer.Web;
using EPiServer.Core;

namespace EPiServer.Research.UI.CssEditor
{
    [PagePlugIn("CSSEditorRightClick", "Right click option for CSS")]
    public class RightClickPageExtension : EPiServer.UserControlBase
    {
        public RightClickPageExtension(PageBase page)
        {
            page.PreRender += new EventHandler(page_PreInit);
        }

        private DataRow[] FindCssForPage(int p)
        {
            DataSet ds = CssDB.loadSetting();
            DataRow[] dr = ds.Tables[0].Select("pageid=" + p);
            return dr;
        }

        

        public static void Initialize(int optionFlag)
        {
            PageBase.PageSetup += new PageSetupEventHandler(setupURi);
        }

        private static void setupURi(PageBase page, PageSetupEventArgs e)
        {
            RightClickPageExtension rccss = new RightClickPageExtension(page);
        }
        void page_PreInit(object sender, EventArgs e)
        {
            //render the page 
            Page page = sender as Page;

            //if page does not exist
            if (page == null)
                return;

            if (page.Header == null)
                return;                              

            EPiServer.UI.SystemPageBase sys = sender as EPiServer.UI.SystemPageBase;
            if (sys != null)
                return;
           
            EPiServer.TemplatePage p = page as TemplatePage;
            EPiServer.PageBase pb = (EPiServer.PageBase)page;

            if (p != null)
            {
                string translationPrefix = "/csseditor/rightclickmenu/";
                if (EPiServer.Security.PrincipalInfo.HasEditAccess)
                {
                    RightClickMenu EPiCSSSubMenu = new RightClickMenu();
                    EPiCSSSubMenu.Add("StartEdit", EPiServer.Security.AccessLevel.Edit, new EPiServer.RightClickMenuItem(LanguageManager.Instance.Translate(translationPrefix + "startedit"), "showcss()", RightClickMode.View));
                    EPiCSSSubMenu.Add("UndoCss", EPiServer.Security.AccessLevel.Edit, new EPiServer.RightClickMenuItem(LanguageManager.Instance.Translate(translationPrefix + "undolastchange"), "undocss()", RightClickMode.View));
                    EPiCSSSubMenu.Add("RemoveCss", EPiServer.Security.AccessLevel.Edit, new EPiServer.RightClickMenuItem(LanguageManager.Instance.Translate(translationPrefix + "removecssmodifications"), "removecss()", RightClickMode.View));
                    EPiCSSSubMenu.Add("SaveEdit", EPiServer.Security.AccessLevel.Edit, new EPiServer.RightClickMenuItem(LanguageManager.Instance.Translate(translationPrefix + "savecss"), "savecss()", RightClickMode.View));
                    ((EPiServer.PageBase)(page)).ContextMenu.Menu.Add("Edit style", EPiServer.Security.AccessLevel.Edit, new RightClickMenuItem(LanguageManager.Instance.Translate(translationPrefix + "submenutitle"), "Nada()", RightClickMode.View));
                    if (((EPiServer.PageBase)(page)).ContextMenu.Menu.Items["Edit style"] != null)
                    {
                        ((EPiServer.PageBase)(page)).ContextMenu.Menu.Items["Edit style"].SubMenu = EPiCSSSubMenu;
                        //string scriptcode = "<script src='" + Settings.Instance.SiteUrl.ToString() + "csseditor/scripts/csseditor.js'></script>";
                        HtmlGenericControl hgcRoot = new HtmlGenericControl("script");
                        hgcRoot.InnerText = "\nvar _appRoot='" + Settings.Instance.SiteUrl.ToString()+"';\n";
                        hgcRoot.InnerText += "\nvar _currentPageId='" + pb.CurrentPageLink.ID.ToString() + "';\n";
                        page.Header.Controls.Add(hgcRoot);
                        HtmlGenericControl hl = new HtmlGenericControl("script");
                        hl.Attributes.Add("src", Settings.Instance.SiteUrl.ToString() + "csseditor/scripts/csseditor.js");
                        page.Header.Controls.Add(hl);

                        HtmlGenericControl jqueryscript = new HtmlGenericControl("script");
                        jqueryscript.Attributes.Add("src", Settings.Instance.SiteUrl.ToString() + "csseditor/scripts/jquery.js");
                        page.Header.Controls.Add(jqueryscript);


                        HtmlGenericControl jqueryuiscript = new HtmlGenericControl("script");
                        jqueryuiscript.Attributes.Add("src", Settings.Instance.SiteUrl.ToString() + "csseditor/scripts/jquery-ui.js");
                        page.Header.Controls.Add(jqueryuiscript);

                        HtmlGenericControl jquerycssscript = new HtmlGenericControl("script");
                        jquerycssscript.Attributes.Add("src", Settings.Instance.SiteUrl.ToString() + "csseditor/scripts/jquery.csseditor.js");
                        page.Header.Controls.Add(jquerycssscript);

                        HtmlGenericControl jquerytheme = new HtmlGenericControl("link");
                        jquerytheme.Attributes.Add("href", Settings.Instance.SiteUrl.ToString() + "csseditor/scripts/themes/ui.all.css");
                        jquerytheme.Attributes.Add("rel","stylesheet");
                        jquerytheme.Attributes.Add("type", "text/css");
                        page.Header.Controls.Add(jquerytheme);
                        HtmlGenericControl jquerycsstheme = new HtmlGenericControl("link");
                        jquerycsstheme.Attributes.Add("href", Settings.Instance.SiteUrl.ToString() + "csseditor/scripts/themes/ui.csseditor.css");
                        jquerycsstheme.Attributes.Add("rel", "stylesheet");
                        jquerycsstheme.Attributes.Add("type", "text/css");
                        page.Header.Controls.Add(jquerycsstheme);

                        //page.ClientScript.RegisterStartupScript(typeof(RightClickPageExtension), "csseditorscript", scriptcode, false);
                    }
                }

                
                DataRow[] drs = FindCssForPage(pb.CurrentPageLink.ID);
                if (drs.Length > 0)
                {
                    string styletext = "";
                    foreach (DataRow dr in drs)
                    {
                        styletext += "\n"+dr["classname"] + "\n{\n" + dr["classstring"] + "\n}\n";
                    }
                    HtmlGenericControl hgc = new HtmlGenericControl("style");
                    hgc.InnerText = styletext;
                   
                    page.Header.Controls.Add(hgc);
                    // add javascript to enable undo style editing
                    if (EPiServer.Security.PrincipalInfo.HasEditAccess)
                    {
                        string scripttext = "";
                        foreach (DataRow dr in drs)
                        {                            
                            scripttext += "\nappliedcss[appliedcss.length]=new myrule('" + dr["classname"] + "','" + dr["classstring"] + "');\n";
                        }
                        HtmlGenericControl hgcscript = new HtmlGenericControl("script");
                        hgcscript.InnerText = scripttext;
                        page.Header.Controls.Add(hgcscript);
                    }
                }
            }               
            
        }
    }
}
